package com.example.dllo.my36kr.main;


import android.content.Intent;
import android.graphics.Color;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;

import com.example.dllo.my36kr.R;
import com.example.dllo.my36kr.drawer.DrawerAdapter;
import com.example.dllo.my36kr.drawer.DrawerBean;
import com.example.dllo.my36kr.find.FindFragment;
import com.example.dllo.my36kr.message.MessageFragment;
import com.example.dllo.my36kr.message.enter.EnterAty;
import com.example.dllo.my36kr.my.MyFragment;
import com.example.dllo.my36kr.my.MyUser;
import com.example.dllo.my36kr.news.NewsFragment;
;
import com.example.dllo.my36kr.share.ShareFragment;
import com.example.dllo.my36kr.tools.BaseAty;
import com.example.dllo.my36kr.tools.MyApp;
import com.example.dllo.my36kr.tools.StaticUrl;

import java.util.ArrayList;

import cn.bmob.v3.Bmob;
import cn.bmob.v3.BmobUser;


public class MainActivity extends BaseAty implements View.OnClickListener {
//http://www.jianshu.com/p/adf7a994613a    TabLayout监听

    private ArrayList<Fragment> fragments;
    private TabLayout tabLayout;
    private ViewPager viewPager;
    private MainAdapter adapter;//viewPager 的adapter
    private DrawerLayout drawerLayout;
    private ArrayList<DrawerBean> list;
    private ListView listView;
    private DrawerAdapter drawerAdapter;
    private LinearLayout layout;
    private NewsFragment fragment;
    private Intent intent;
    private Button returnBnt;
    private MyUser myUser;


    @Override
    protected int setLayout() {

        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        tabLayout = bindView(R.id.tl_main);
        viewPager = bindView(R.id.vp_main);
        adapter = new MainAdapter(getSupportFragmentManager());
        listView = bindView(R.id.lv_main_chouti);
        drawerLayout = bindView(R.id.dl_main);//抽屉的Id
        drawerAdapter = new DrawerAdapter(getApplicationContext());
        layout = bindView(R.id.ll_main);// 抽屉  android:layout_gravity="left"
        layout.bringToFront();//让点击才消失
        returnBnt = bindView(R.id.bnt_main_fanhui);//返回键
        returnBnt.setOnClickListener(this);


    }

    public void openMenu() {
        drawerLayout.openDrawer(Gravity.LEFT);//抽屉从哪个方向划出

    }

    public void unLockedMenu() {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); //打开手势滑动
    }

    public void lockedMenu() {
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); //关闭手势滑动
    }


    @Override
    protected void initData() {
        fragment = new NewsFragment();
        list = new ArrayList<>();
        fragments = new ArrayList<>();

        fragments.add(new NewsFragment());//新闻
        fragments.add(new ShareFragment());//股份投资
        fragments.add(new FindFragment());//发现
        fragments.add(new MessageFragment());//消息
        fragments.add(new MyFragment());//我的
        adapter.setFragments(fragments);
        viewPager.setAdapter(adapter);
        tabLayout.setupWithViewPager(viewPager);
        tabLayout.getTabAt(0).setIcon(R.drawable.news);
        tabLayout.getTabAt(1).setIcon(R.drawable.share);
        tabLayout.getTabAt(2).setIcon(R.drawable.find);
        tabLayout.getTabAt(3).setIcon(R.drawable.message);
        tabLayout.getTabAt(4).setIcon(R.drawable.my);
        tabLayout.setTabTextColors(Color.rgb(0x98, 0x9d, 0xa8), Color.rgb(0x6c, 0x8f, 0xff));
        tabLayout.setSelectedTabIndicatorColor(Color.WHITE);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {//viewpager 监听 实现在哪页划出抽屉
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

                if (position == 0) {//viewPager的位置
                    unLockedMenu();
                } else {
                    lockedMenu();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {


            }
        });
        list = new ArrayList<>();
        list.add(new DrawerBean("全部", R.mipmap.quanbuo));
        list.add(new DrawerBean("项目早期", R.mipmap.zaoqi));
        list.add(new DrawerBean("B轮后", R.mipmap.lun));
        list.add(new DrawerBean("大公司", R.mipmap.dagongsi));
        list.add(new DrawerBean("资本", R.mipmap.ziben));
        list.add(new DrawerBean("深度", R.mipmap.shendu));
        list.add(new DrawerBean("研究", R.mipmap.yanju));
        list.add(new DrawerBean("氪TV", R.mipmap.ketv));
        drawerAdapter.setList(list);
        listView.setAdapter(drawerAdapter);


        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {//点击事件

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                intent = new Intent();
                switch (position) {
                    case 0:
                        intent.setAction("b");
                        intent.putExtra("all", StaticUrl.NEWS_DATA_URL);
                        break;
                    case 1:
                        intent.setAction("a");
                        intent.putExtra("all", "67");
                        break;
                    case 2:
                        intent.setAction("a");
                        intent.putExtra("all", "68");
                        break;
                    case 3:
                        intent.setAction("a");
                        intent.putExtra("all", "23");
                        break;
                    case 4:
                        intent.setAction("a");
                        intent.putExtra("all", "69");
                        break;
                    case 5:
                        intent.setAction("a");
                        intent.putExtra("all", "70");
                        break;
                    case 6:
                        intent.setAction("a");
                        intent.putExtra("all", "71");
                        break;
//                    case 7:
//                        intent.setAction("c");
//                        intent.putExtra("all", StaticUrl.NEWS_KRTY_URL);
//                        break;

                }
                drawerLayout.closeDrawers();
                sendBroadcast(intent);

            }

        });
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab == tabLayout.getTabAt(3)) {//tablayout的监听
                    if (myUser != null) {
                        tabLayout.getTabAt(3);
                    } else {
                        startActivity(new Intent(MainActivity.this, EnterAty.class));
                    }
                }

            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    @Override
    protected void onStart() {
        super.onStart();
        myUser = BmobUser.getCurrentUser(MyUser.class);
    }

    public void replaceOne() {
        viewPager.setCurrentItem(1);
    }


    @Override
    public void onClick(View v) {
        drawerLayout.closeDrawers();
    }


}
